<template>
  <!-- 这里是评论组件哦 -->
  <van-cell class="comment-item">
    <van-image
      slot="icon"
      class="avatar"
      round
      fit="cover"
      :src="comment.aut_photo"
    />
    <div slot="title" class="title-wrap">
      <div class="user-name">{{ comment.aut_name }}</div>
      <!-- 点赞的点击事件 -->
      <van-button
        class="like-btn"
        icon="good-job-o"
        @click="onCommentLike()"
        :class="{
          liked: comment.is_liking,
        }"
        :loading="loading"
        >{{ comment.like_count || "赞" }}</van-button
      >
    </div>

    <div slot="label">
      <p class="comment-content">{{ comment.content }}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{
          comment.pubdate | relativeTime
        }}</span>
        <van-button class="reply-btn" round @click="$emit('replyClick',comment)"
          >回复 {{ comment.reply_count }}</van-button
        >
      </div>
    </div>
  </van-cell>
</template>

<script>
import { deleteCommentLikeAPI, addCommentLikeAPI } from '@/api/index.js'
export default {
  name: 'CommentItem',
  // 接收父组件传递过来的值---> 评论
  props: {
    comment: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      loading: false,
      commentClone: this.comment // 将父组件传递过来的数据复制一份
    }
  },
  methods: {
    // 评论点赞
    async onCommentLike () {
      // 调用接口的时候进行加载中
      this.loading = true
      try {
        // 判断,如果已经点赞,点击取消点赞 没有的话,点击点赞
        if (this.comment.is_liking) {
          await deleteCommentLikeAPI(this.comment.com_id)
        } else {
          await addCommentLikeAPI(this.comment.com_id)
          // 点赞之后 赞数在当前数上加1
          this.commentClone.like_count++
        }
        this.commentClone.is_liking = !this.comment.is_liking
      } catch (error) {
        this.$toast('操作失败，请重试')
      }
      this.loading = false
    }

  }
}
</script>

<style lang="less" scoped>
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
    &.linked{
        color: #da4622;
    }
  }
}
</style>
